package com.cykj.pos.util;

import com.cykj.pos.domain.BizShareDetail;
import com.cykj.pos.profit.dto.ShareDetailDTO;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 分润明细规则util
 */
public class ShareDetailUtil {
    // 交易分润
    public static BizShareDetail dealShare(BigDecimal monthVolume){
        BizShareDetail bizShareDetail = new BizShareDetail();
        // 定义分润比较的档次
        BigDecimal zero = new BigDecimal("0");
        BigDecimal one = new BigDecimal("500000");
        BigDecimal two = new BigDecimal("3000000");
        BigDecimal three = new BigDecimal("7000000");
        BigDecimal four = new BigDecimal("10000000");
        BigDecimal shareResult = null;
        // w3
        if(monthVolume.compareTo(one)==-1 || monthVolume.compareTo(one)==0 || monthVolume.compareTo(zero)==0){
            BigDecimal w3 = new BigDecimal("0.0003");
            shareResult = monthVolume.multiply(w3);// 算分润
            bizShareDetail.setDealShare(shareResult);// 存分润
            bizShareDetail.setDealShareRatio(w3);// 存分润率
            return bizShareDetail;
        }
        // w5
        if(monthVolume.compareTo(two)==-1 || monthVolume.compareTo(two)==0){
            BigDecimal w5 = new BigDecimal("0.0005");
            shareResult = monthVolume.multiply(w5);
            bizShareDetail.setDealShare(shareResult);
            bizShareDetail.setDealShareRatio(w5);
            return bizShareDetail;
        }
        // w6
        if(monthVolume.compareTo(three)==-1 || monthVolume.compareTo(three)==0){
            BigDecimal w6 = new BigDecimal("0.0006");
            shareResult = monthVolume.multiply(w6);
            bizShareDetail.setDealShare(shareResult);
            bizShareDetail.setDealShareRatio(w6);
            return bizShareDetail;
        }
        // w7
        if(monthVolume.compareTo(four)==-1 || monthVolume.compareTo(four)==0){
            BigDecimal w7 = new BigDecimal("0.0007");
            shareResult = monthVolume.multiply(w7);
            bizShareDetail.setDealShare(shareResult);
            bizShareDetail.setDealShareRatio(w7);
            return bizShareDetail;
        }
        // w8
        BigDecimal w8 = new BigDecimal("0.0008");
        shareResult = monthVolume.multiply(w8);
        bizShareDetail.setDealShare(shareResult);
        bizShareDetail.setDealShareRatio(w8);
        return bizShareDetail;
    }

    // 交易分润
    public static BizShareDetail dealRatio(BigDecimal monthVolume){
        BizShareDetail bizShareDetail = new BizShareDetail();
        // 定义分润比较的档次
        BigDecimal zero = new BigDecimal("0");
        BigDecimal one = new BigDecimal("500000");
        BigDecimal two = new BigDecimal("3000000");
        BigDecimal three = new BigDecimal("7000000");
        BigDecimal four = new BigDecimal("10000000");
        BigDecimal shareResult = null;
        // w3
        if(monthVolume.compareTo(one)==-1 || monthVolume.compareTo(one)==0 || monthVolume.compareTo(zero)==0){
            BigDecimal w3 = new BigDecimal("0.0003");
            bizShareDetail.setDealShareRatio(w3);// 存分润率
            return bizShareDetail;
        }
        // w5
        if(monthVolume.compareTo(two)==-1 || monthVolume.compareTo(two)==0){
            BigDecimal w5 = new BigDecimal("0.0005");
            bizShareDetail.setDealShareRatio(w5);
            return bizShareDetail;
        }
        // w6
        if(monthVolume.compareTo(three)==-1 || monthVolume.compareTo(three)==0){
            BigDecimal w6 = new BigDecimal("0.0006");
            bizShareDetail.setDealShareRatio(w6);
            return bizShareDetail;
        }
        // w7
        if(monthVolume.compareTo(four)==-1 || monthVolume.compareTo(four)==0){
            BigDecimal w7 = new BigDecimal("0.0007");
            bizShareDetail.setDealShareRatio(w7);
            return bizShareDetail;
        }
        // w8
        BigDecimal w8 = new BigDecimal("0.0008");
        bizShareDetail.setDealShareRatio(w8);
        return bizShareDetail;
    }

    // 线下一层伙伴的交易钱数
    public static List<BigDecimal> parentDealShare(List<ShareDetailDTO> list){
        List<BigDecimal> list1 = new ArrayList<BigDecimal>();
        for(int i=0;i<list.size();i++){// 线下伙伴不是一个人 循环
            BigDecimal bigDecimal = list.get(i).getDealSum();// 得到该伙伴的交易额
            BizShareDetail bizShareDetail = ShareDetailUtil.dealShare(bigDecimal);// 调用分润方法
            list1.add(i,bizShareDetail.getDealShare());
        }
        return list1;
    }

    // 管理津贴
    public static BigDecimal managerAllowance(List<ShareDetailDTO> list){
        // 管理津贴规则：当有两名或两名以上合伙人月交易流水超过1000万元时，奖励1/万分润，每名
        // 每月2000元封顶
        // 1、线下合伙人 交易额分开判
        BigDecimal bigDecimal = new BigDecimal("10000000");
        BigDecimal allowanceRate = new BigDecimal("0.0001");
        BigDecimal greater = new BigDecimal("2000");
        BigDecimal zero = new BigDecimal("0");
        int personNum = 0;
        for(int i=0;i<list.size();i++){
            if(list.get(i).getDealSum().compareTo(bigDecimal)==1){// 说明大于1000万
                personNum++;
            }
        }
        BigDecimal allowance = new BigDecimal("0");
        if (personNum>=2){
            // 人数超过才参与分润
            for (int i=0;i<list.size();i++){
                if(list.get(i).getDealSum().compareTo(bigDecimal)==1){
                    allowance = allowance.add(list.get(i).getDealSum().multiply(allowanceRate));// 计算管理津贴的钱
                }
            }
            if (allowance.compareTo(greater)==1){// 判断是否超过2000
                // 如果超过就返回2000
                return greater;
            }
            // 没超过2000返回正常的
            return allowance;
        }
        // 没有超过1000万的或者是 没超过两个人
        return zero;
    }

    // 钻石奖金
    public static BigDecimal diamondBonus(List<ShareDetailDTO> list){
        // 钻石奖金规则：管理合伙人的一名合伙人月交易流水超过2000万时，改名合伙人成为一个达标合伙人
        // 按人数计算奖金
        BigDecimal bigDecimal = new BigDecimal("20000000");
        BigDecimal three = new BigDecimal("500");// 3人及以上
        BigDecimal six = new BigDecimal("1000");// 6人及以上
        BigDecimal nine = new BigDecimal("1500");// 9人及以上
        BigDecimal twelve = new BigDecimal("2000");// 12人及以上
        BigDecimal zero = new BigDecimal("0");
        BigDecimal diamond = new BigDecimal("0");
        int personNum = 0;
        for (int i=0;i<list.size();i++){
            if(list.get(i).getDealSlotCardSum().compareTo(bigDecimal)==1){// 交易额大于2000万
                personNum++;
            }
        }
        if(personNum<3){
            return zero;// 小于3个人 返回0
        }
        if (personNum>=3 && personNum<6){
            diamond = three.multiply(BigDecimal.valueOf((int)personNum));
        }
        if (personNum>=6 && personNum<9){
            diamond = six.multiply(BigDecimal.valueOf((int)personNum));
        }
        if (personNum>=9 && personNum<12){
            diamond = nine.multiply(BigDecimal.valueOf((int)personNum));
        }
        if (personNum>=12){
            diamond = twelve.multiply(BigDecimal.valueOf((int)personNum));
        }
        return diamond;
    }

    // 皇冠奖
    public static BizShareDetail crownAward(BigDecimal bigDecimal){
       BigDecimal one = new BigDecimal("100000000");
       BigDecimal tow = new BigDecimal("200000000");
       BigDecimal four = new BigDecimal("400000000");
       BigDecimal eight = new BigDecimal("800000000");
       BigDecimal zero = new BigDecimal("0");
       BizShareDetail bizShareDetail = new BizShareDetail();
       BigDecimal crownMoney = null;
       if(bigDecimal.compareTo(one)==-1){
           bizShareDetail.setCrownAward(zero);
           bizShareDetail.setCrownRatio(zero);
           return bizShareDetail;
       }
       if (bigDecimal.compareTo(tow)==-1 || bigDecimal.compareTo(one)==0){
           BigDecimal w1 = new BigDecimal("0.00008");
           crownMoney = bigDecimal.multiply(w1);
           bizShareDetail.setCrownAward(crownMoney);
           bizShareDetail.setCrownRatio(w1);
           return bizShareDetail;
       }
       if (bigDecimal.compareTo(four)==-1 || bigDecimal.compareTo(tow)==0){
           BigDecimal w2 = new BigDecimal("0.00015");
           crownMoney = bigDecimal.multiply(w2);
           bizShareDetail.setCrownAward(crownMoney);
           bizShareDetail.setCrownRatio(w2);
           return bizShareDetail;
       }
        if (bigDecimal.compareTo(eight)==-1 || bigDecimal.compareTo(four)==0){
            BigDecimal w3 = new BigDecimal("0.00022");
            crownMoney = bigDecimal.multiply(w3);
            bizShareDetail.setCrownAward(crownMoney);
            bizShareDetail.setCrownRatio(w3);
            return bizShareDetail;
        }
        BigDecimal w4 = new BigDecimal("0.0003");
        crownMoney = bigDecimal.multiply(w4);
        bizShareDetail.setCrownAward(crownMoney);
        bizShareDetail.setCrownRatio(w4);
        return bizShareDetail;
    }

    // 分红奖励
    public static BigDecimal participation(List<ShareDetailDTO> list,BigDecimal bigDecimal){
        BigDecimal w1 = new BigDecimal("2500");
        BigDecimal w2 = new BigDecimal("5000");
        BigDecimal w3 = new BigDecimal("7500");
        BigDecimal w4 = new BigDecimal("10000");
        BigDecimal one = new BigDecimal("50000000");
        BigDecimal two = new BigDecimal("100000000");
        BigDecimal three = new BigDecimal("200000000");
        BigDecimal four = new BigDecimal("300000000");
        BigDecimal millionsTeam = new BigDecimal("10000000");
        BigDecimal zero = new BigDecimal("0");
        int team = 0;
        if (bigDecimal.compareTo(one)==-1){
            return zero;
        }
        if (bigDecimal.compareTo(two)==-1 || bigDecimal.compareTo(one)==0){
            for (int i=0;i<list.size();i++){
                if (list.get(i).getDealSum().compareTo(millionsTeam)==1 || list.get(i).getDealSum().compareTo(millionsTeam)==0){
                    team++;
                }
            }
            if (team>=2){
                return w1;
            }
            return zero;
        }
        if (bigDecimal.compareTo(three)==-1 || bigDecimal.compareTo(two)==0){
            for (int i=0;i<list.size();i++){
                if (list.get(i).getDealSum().compareTo(millionsTeam)==1 || list.get(i).getDealSum().compareTo(millionsTeam)==0){
                    team++;
                }
            }
            if (team>=3){
                return w2;
            }
            if (team==2){
                return w1;
            }
            return zero;
        }
        if (bigDecimal.compareTo(four)==-1 || bigDecimal.compareTo(three)==0){
            for (int i=0;i<list.size();i++){
                if (list.get(i).getDealSum().compareTo(millionsTeam)==1 || list.get(i).getDealSum().compareTo(millionsTeam)==0){
                    team++;
                }
            }
            if (team>=4){
                return w3;
            }
            if (team==3){
                return w2;
            }
            if (team==2){
                return w1;
            }
            return zero;
        }
        if (bigDecimal.compareTo(four)==1 || bigDecimal.compareTo(four)==0){
            for (int i=0;i<list.size();i++){
                if (list.get(i).getDealSum().compareTo(millionsTeam)==1 || list.get(i).getDealSum().compareTo(millionsTeam)==0){
                    team++;
                }
            }
            if (team>=5){
                return w4;
            }
            if (team==4){
                return w3;
            }
            if (team==3){
                return w2;
            }
            if (team==2){
                return w1;
            }
            return zero;
        }
        return w4;
    }

    // 测试
    public static void main(String[] args) {
       /*ShareDetailDTO shareDetailDTO = new ShareDetailDTO();
       ShareDetailDTO shareDetailDTO1 = new ShareDetailDTO();
       ShareDetailDTO shareDetailDTO2 = new ShareDetailDTO();
       List<ShareDetailDTO> list = new ArrayList<ShareDetailDTO>();
       shareDetailDTO.setDealSum(new BigDecimal("30000000"));
       shareDetailDTO1.setDealSum(new BigDecimal("31000000"));
       shareDetailDTO2.setDealSum(new BigDecimal("41000000"));
       list.add(shareDetailDTO);
       list.add(shareDetailDTO1);
       list.add(shareDetailDTO2);
       BigDecimal bigDecimal = new BigDecimal("100000000");
       BigDecimal bigDecimal1 = ShareDetailUtil.participation(list,bigDecimal);
       System.out.println(bigDecimal1);*/
       String order = ShareDetailUtil.createOrderId();
       System.out.println(order);
    }

    // 生成唯一订单号的方法
    public static String createOrderId(){
        int machineId = 1;//最大支持1-9个集群机器部署
        int hashCodeV = UUID.randomUUID().toString().hashCode();
        if(hashCodeV < 0) {//有可能是负数
            hashCodeV = - hashCodeV;
        }
        // 0 代表前面补充0
        // 4 代表长度为4
        // d 代表参数为正数型
        return machineId+String.format("%015d", hashCodeV);
    }


    // 精度损失问题
    // 加
    /*public static BigDecimal add(double v1, double v2) {
        // v1 + v2
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.add(b2);
    }
    // 减
    public static BigDecimal sub(double v1, double v2) {
        //v1-v2
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.subtract(b2);
    }
    // 乘
    public static BigDecimal mul(double v1, double v2) {
        //v1*v2
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.multiply(b2);
    }
    // 除
    public static BigDecimal div(double v1, double v2) {
        //v1/v2
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        // 2 = 保留小数点后两位   ROUND_HALF_UP = 四舍五入
        return b1.divide(b2, 2, BigDecimal.ROUND_HALF_UP);// 应对除不尽的情况
    }*/

}
